﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlSector
    {
        private ConexionDataContext conexion;

        internal List<CONEXION.SECTOR> getListSector()
        {
            using (conexion = new ConexionDataContext())
            {
                var query = conexion.SECTOR.Where(x => x.eliminado == 0).ToList();
                return query; 
            }
        }

        internal System.Windows.Forms.DialogResult registrarSector(SECTOR sec)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SECTOR sector = sec;
                        DialogResult result = DialogResult.Cancel;

                        //insertamos el id
                        int correlativoSector = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").SECTOR.Value;
                        string num = Convert.ToString(correlativoSector);
                        string numero = num.Trim().PadLeft(10, '0');
                        string idSector = "SECT_" + numero;

                        sec.idSector = idSector;

                        //insertamos el sector
                        conexion.SECTOR.InsertOnSubmit(sec);
                        conexion.SubmitChanges();
                        conexion.increm_ContSector();

                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }  
            }
        }

        internal System.Windows.Forms.DialogResult actualizarSector(SECTOR sec)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SECTOR sect = conexion.SECTOR.Single(x => x.idSector == sec.idSector);
                        DialogResult result = DialogResult.Cancel;
                        //actualizamos
                        sect.nombreSector = sec.nombreSector;
                        sect.eliminado = sec.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarSector(SECTOR sec)
        {
            using (conexion = new ConexionDataContext())
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        CONEXION.SECTOR sect = conexion.SECTOR.Single(x => x.idSector == sec.idSector);
                        //actualizamos
                        sect.eliminado = sec.eliminado;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal List<SECTOR> buscarSectorExacto(string columna, string value)
        {
            using (conexion = new ConexionDataContext())
            {
                List<SECTOR> result = new List<SECTOR>();

                try
                {
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        switch (columna)
                        {
                            case "nombreSector":
                                result = conexion.SECTOR.Where(x => x.nombreSector == value).OrderBy(x => x.nombreSector).ToList();
                                break;
                        }
                    }

                    return result;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}
